const express = require('express')
const app = express()
const port = 3000

const db = require('./SQLHelper')

var bodyParser = require('body-parser')
require('body-parser-xml')(bodyParser)
app.use(bodyParser.json({ limit:'100mb' }))
app.use(bodyParser.urlencoded({ limit:'100mb', extended: true }))

app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", '*')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
  res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type,token');
  next()
})

app.get('/getTest', (req, res) => {
  res.send({ msg:'Get Hello World!' })
})

app.post('/login',async (req, res) => {
  let userName = req.body.userName,passWord = req.body.passWord
  if(!userName || !passWord) {
    res.send({ errorMsg:'请输入用户名和密码' })
    return
  }
  let user = await db.query(`select * from user where userName="${userName}";`)
  if(!user[0]) {
    res.send({ errorMsg:'用户名不存在' })
    return
  }
  user = user[0]
  if(user.passWord == passWord){
    res.send({ msg:'登录成功' })
  }else{
    res.send({ errorMsg:'密码错误' })
  }    
})  

app.get('/user/:id', (req, res) => {
  let params = req.params  
  let header = req.headers
  res.send({msg:'aaa'})
})

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})